package cn.wolfcode.luowowo.controller;


import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParamLogin;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.TravelCommentQuery;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/travels")
public class TravelController {


    @Autowired
    private ITravelService travelService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private ITravelCommentService travelCommentService;

    @GetMapping("/query")
    public Object query(TravelQuery qo){
        Page<Travel> query = travelService.query(qo);

        return JsonResult.success(query);
    }


    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }


    /**
     * 编辑回显
     * @param id
     * @return
     * 如果有Id 才能游记对象
     */
    @GetMapping("/input")
    public Object input(String id){
        HashMap map = new HashMap();
        if (StringUtils.hasLength(id)) {
            Travel tv = travelService.get(id);
            map.put("tv",tv);
        }
        List<Destination> dests = destinationService.list();

        map.put("dests",dests);

        return JsonResult.success(map);
    }


    /**
     * 找到当前游记对象
     * // 里面缺的属性记得补上
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Object detail(String id){
        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }



    @PostMapping("/saveOrUpdate")
    // 没登陆不能进入
    @RequireLogin
    public Object saveOrUpdate(Travel travel,HttpServletRequest request){
        // 通过请求路径拿到请求头
        String token = request.getHeader("token");
        // 在redis中找到当前登陆对象
        UserInfo userInfo = userInfoRedisService.getToken(token);
        // set回新增的游记的作者对象中
        travel.setUserId(userInfo.getId());
        if (StringUtils.hasLength(travel.getId())){
            travelService.update(travel);
        }else {
            travelService.save(travel);
        }
        // 因为新增编辑完就会回到你写的这篇游记明细页面 需要传一个.然后他会找过去
        return JsonResult.success(travel.getId());
    }

    /**
     * 游记评论新增
     * @param travelComment
     * @param userInfo
     * @return
     */
    @PostMapping("/commentAdd")
    @RequireLogin // 登陆才能评论
    public Object commentAdd(TravelComment travelComment, @UserParamLogin UserInfo userInfo){

        if (userInfo != null){
            // 数据转移
            BeanUtils.copyProperties(userInfo,travelComment);
            // 属性名字不同要手动 set
            travelComment.setUserId(userInfo.getId());
            travelComment.setCreateTime(new Date());
        }
        // 执行save 方法
        travelCommentService.save(travelComment);
        return JsonResult.success();
    }


    /**
     * 根据游记的id 拿到 游记评论 (不分页)
     * @param
     * @return
     */
    @GetMapping("/comments")
    public Object comments(TravelCommentQuery qo){
        List<TravelComment> query = travelCommentService.query(qo);
        return JsonResult.success(query);
    }



}
